package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.common.utils.http.HttpUtils;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
public class WechatServiceImpl implements WechatService {
    @Value("${weixin.appId}")
    private String appId;
    @Value("${weixin.appSecret}")
    private String appSecret;

    //获取手机号API地址
    private static final String POST_PHONE_API_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    //小程序登录API地址
    private static final String GET_OPEN_ID_URL = "https://api.weixin.qq.com/sns/jscode2session";

    //获取访问Token的API地址
    private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";

    public Map<String, Object> getAppConfig(){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("appid",appId);
        paramMap.put("secret",appSecret);
        return paramMap;
    }

    private String getToken(){
        Map<String, Object> paramMap = this.getAppConfig();
        paramMap.put("grant_type","client_credential");
        String result = HttpUtil.get(GET_ACCESS_TOKEN_URL,paramMap);
        if(StringUtils.isBlank(result)){
            throw  new BaseException("调用微信接口生成通行证token失败");
        }
        JSONObject jsonObject = JSON.parseObject(result);
        String token= jsonObject.getString("access_token");
        return token;
    }
    @Override
    public String getOpenId(String code) {
        Map<String,Object> paramMap = getAppConfig();
        paramMap.put("grant_type","authorization_code");//授权类型
        paramMap.put("js_code",code);
        String result = HttpUtil.get(GET_OPEN_ID_URL,paramMap);
        if(StringUtils.isBlank(result)){
            throw new BaseException("调用微信接口生成openid失败");
        }
        JSONObject jsonObject = JSON.parseObject(result);
        String openid = jsonObject.getString("openid");
        return openid;
    }

    @Override
    public String getPhone(String code) {
       //准备参数
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("code",code);
        String token = getToken();
        String result= HttpUtil.post(POST_PHONE_API_URL+token,JSON.toJSONString(paramMap));
        if(StringUtils.isBlank(result)){
            throw new BaseException("生成电话号码失败");
        }
        JSONObject jsonObject = JSON.parseObject(result);
        String phoneInfo = jsonObject.getString("phone_info");
        if(StringUtils.isNotBlank(phoneInfo)){
            JSONObject json = JSON.parseObject(phoneInfo);
            String phoneNumber = json.getString("phoneNumber");
            return phoneNumber;
        }
        return null;
        //调用接口


    }
}
